//
//  HSClassRoomController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit
//通知刷新的闭包

typealias classRoomCallBack = (_ error :Error? ,_ isRearsh: Bool) -> ()



class HSClassRoomController: HSBasicController {
 
    var dataArray : [HSClassRoomModel] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = 100
        //注册原形cell 
        tableView.register(HSClassRoomCell.self, forCellReuseIdentifier: "HSClassRoomCell")
       //获取数据
        loadData()
    }
   //重写父类方法
    override func loadData() {
        //开始刷新
        refreshC.beginRefreshing()
        
        //获得刷新类型
        let loType = isPull ? LoadType.pull : LoadType.drop
        //请求数据
        requestClassRoomData(classRoomLoadType: loType) {[weak self] (err, isReafresh) in
            //关闭刷新控件
            self?.refreshC.endRefreshing()
            //记录刷新
            self?.isPull = false
            //判断是否刷新视图
            if err != nil{
                return
            }
            if isReafresh {
                //刷新视图
                self?.tableView.reloadData()
            }
        
        }
        
    }
    //获取数据
    func requestClassRoomData(classRoomLoadType : LoadType,finished :@escaping classRoomCallBack){
        //获取地址接口
        let url = "ZH-schoolname-S-getNewMessage"
        //参数设置
        guard let key = userInfo["key"] as? String else{
            return
        }
        //获得当前最小的通知ID
        let minID = dataArray.last?.id ?? 0
        //根据刷新类型来获得通知的ID
        let id = (classRoomLoadType == .drop) ? 0 : minID
        //根据刷新类型来获得通知的ID
        let loType = (classRoomLoadType == .drop) ? 1 : 2
        let parame = ["key":key,"id":id,"load_type":loType] as [String : Any]
        //请求数据
        HSNetwordTools.shared.downloadData(urlStr: url, parameters: parame) { [weak self](data, error) in
           
            guard let data = data as? NSDictionary else{
                HSAlertView.bottomAlertView("网络异常,请检查网络")
                finished(error, false)
                return
            }
            switch data["result"] as! Int{
            case 0 :
                //用一个数组来先保存数组模型
                let array = data["data"] as! [[String : Any]]
                //创建一个可变数组来接收模型
                var modelArray = [HSClassRoomModel]()
                //遍历取出模型
                for dict in array{
                    let model = HSClassRoomModel(dict: dict)
                    modelArray.append(model)
                }
                //赋值
                self?.dataArray = modelArray
                //刷新视图
                finished(nil, true)
            case 1 :
                HSAlertView.bottomAlertView("没有更多的作业了")
                finished(nil, false)
            default :
                break
            }
            
            
        }
    }



     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }

  
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HSClassRoomCell", for: indexPath) as! HSClassRoomCell
        cell.classRoomVM = dataArray[indexPath.row]
        //设置选中颜色
        cell.selectionStyle = .none

        return cell
    }

}
